﻿define(['lodash'], function (_) {
    return Vue.extend({
        data: function () {
            return {
                page: {
                    skipCount: 0,
                    maxResultCount: 10
                },
                paged: {},
                filter: {}
            }
        },
        methods: {
            getPaged: function (options) {
                var vm = this;
                if (Object.prototype.toString.call(options) === "[object String]") {
                    options = { url: options };
                }
                options = options || {};
                if (!options.url) throw new Error('请传入url接口地址');


                var data = _.extend({
                    page: this.page,
                    filter: this.filter
                }, options.data);

                abp.ui.setBusy(this.el, abp.ajax({
                    url: options.url,
                    data: data,
                    contentType: 'application/x-www-form-urlencoded'
                }).done(function (res) {
                    vm.paged = res;
                    if (options.callback) options.callback();
                }));
            },
            getList: function (apiService, data, success) {
                var vm = this;
                abp.ui.setBusy(this.el, apiService(data, {
                    success: function (res) {
                        vm.paged = res;
                        if (success) success();
                    }
                }));
            },
            confirm: function (msg, callback) {
                abp.message.confirm(msg, function (confirm) {
                    if (confirm) callback();
                })
            },
            remove: function (options) {
                var vm = this;
                this.confirm('您确定要删除吗?', function () { vm.request(options); });
            },
            save: function (options) {
                this.request(options, function () { apb.message.success('保存成功'); });
            },
            query: function (options) {
                this.request(options);
            },
            request: function (options, callback) {
                var vm = this;
                options = options || {};
                if (!options.url) throw new Error('请传入url接口地址');

                abp.ui.setBusy(vm.el, abp.ajax({
                    url: options.url,
                    data: options.data,
                    contentType: 'application/x-www-form-urlencoded'
                }).done(function (res) {
                    if (callback) callback();
                    if (options.callback) options.callback();
                }));
            }
        }
    })
});